package com.gem.service.impl;

import com.gem.domain.CCrCompany;
import com.gem.mapper.CCrCompanyMapper;
import com.gem.service.CCrCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 参与公司接口实现
 * create 2019/10/30 12:00
 * Modified By:
 */
@Service
public class CCrCompanyServiceImpl implements CCrCompanyService {

    //自动注入CCrCompanyMapper
    private CCrCompanyMapper cCrCompanyMapper;

    @Autowired
    public void setcCrCompanyMapper(CCrCompanyMapper cCrCompanyMapper) {
        this.cCrCompanyMapper = cCrCompanyMapper;
    }

    //浏览所有
    @Override
    public List<CCrCompany> selectAllCCService() {
        return cCrCompanyMapper.selectAllCC();
    }

    //按编号查询
    @Override
    public CCrCompany selectCCByIdService(Integer ccId) {
        return cCrCompanyMapper.selectCCByid(ccId);
    }

    //按合作成果编号查询
    @Override
    public List<CCrCompany> selectCCByccCrIdService(Integer ccCrId) {
        return cCrCompanyMapper.selectCCByccCrId(ccCrId);
    }

    //按公司编号查询合作成果编号
    @Override
    public Integer[] selectCCByccCoIdService(Integer ccComId) {
        return cCrCompanyMapper.selectCCByccCoId(ccComId);
    }

    //添加
    @Override
    public boolean addCCService(CCrCompany cCrCompany) {
        if (cCrCompany.getCcTopicUnitprice() == null) {
            cCrCompany.setCcTopicUnitprice(0.00);
        }
        if (cCrCompany.getCcTopicTotalprice() == null) {
            cCrCompany.setCcTopicTotalprice(0.00);
        }
        if (cCrCompany.getCcRemark() == null) {
            cCrCompany.setCcRemark("");
        }
        return cCrCompanyMapper.addCC(cCrCompany);
    }

    //修改
    @Override
    public boolean modifyCCService(CCrCompany cCrCompany) {

        if (cCrCompany.getCcTopicUnitprice() == null) {
            cCrCompany.setCcTopicUnitprice(0.00);
        }
        if (cCrCompany.getCcTopicTotalprice() == null) {
            cCrCompany.setCcTopicTotalprice(0.00);
        }
        if (cCrCompany.getCcRemark() == null) {
            cCrCompany.setCcRemark("");
        }
        return cCrCompanyMapper.modifyCC(cCrCompany);
    }

    //单个删除
    @Override
    public boolean deleteCCService(Integer ccId) {
        return cCrCompanyMapper.deleteCCById(ccId);
    }

    //批量删除（开启事务）
    @Override
    @Transactional
    public boolean deleteCCsService(String[] ccIdArr) {
        try {
            boolean flag = true;
            if (ccIdArr.length != 0) {
                for (String ccId : ccIdArr) {
                    if (!ccId.trim().equals("")) {
                        if (!deleteCCService(Integer.parseInt(ccId))) {
                            flag = false;
                            break;
                        }
                    }
                }
                if (!flag) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public boolean uploadExcelService(List<CCrCompany> list) {
        try {
            boolean flag = true;
            for (CCrCompany cCrCompany : list) {
                if (!cCrCompanyMapper.addCC(cCrCompany)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return true;
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }
}
